$(function () {
    $('form').parsley();
    dynamicBinding();
    getAllForeignMain();
});

//遍历所有foreignMain输出到表格
function getAllForeignMain() {
    let deleteStr = "";
    let updateStr = "";
    let columns = [
        {
            title: "序号", data: null, "render": function (data, type, row, meta) {
                return data = Number(meta.row) + Number(1);
            }
        },
        {title: "基本信息", data: "base"},
        {title: "搜救船次", data: "vesselNumber"},
        {title: "搜救架次", data: "sorties"},
        {
            title: "救助申请", data: "rescueApply", "render": function (data, type, row, meta) {
                let index = data.lastIndexOf("/");
                return '<a href="' + data + '" download="' + data.substring(index + 1, data.length) + '" target="_blank">' + data.substring(index + 1, data.length) + '</a>';
            }
        },
        {
            title: "救助情况", data: "rescueSituation", "render": function (data, type, row, meta) {
                let index = data.lastIndexOf("/");
                return '<a href="' + data + '" download="' + data.substring(index + 1, data.length) + '" target="_blank">' + data.substring(index + 1, data.length) + '</a>';
            }
        },
        {
            title: "救助奖励", data: "rescueReward", "render": function (data, type, row, meta) {
                let index = data.lastIndexOf("/");
                return '<a href="' + data + '" download="' + data.substring(index + 1, data.length) + '" target="_blank">' + data.substring(index + 1, data.length) + '</a>';
            }
        },
        {
            title: "操作", data: "id", "orderable": false, // 禁用排序
            "defaultContent": "", "render": function (data, type, row, meta) {
                if (hasDeletePermission) {
                    deleteStr = "<button data-id=" + data + "  type='button' class='btn btn-danger data-delete'>删除</button>&emsp;"
                }
                if (hasUpdatePermission) {
                    updateStr = "<button data-id=" + data + " type='button' class='btn btn-info data-update' data-toggle='modal' data-target='#updateForeignMainModal'>更新</button>";
                }
                return data = deleteStr + updateStr;
            }
        }
    ];
    let url = 'foreignMain/getAllForeignMain';
    dataTableInit(url, columns);
}

//动态绑定
function dynamicBinding() {
    //为所有的delete按钮绑定delete事件
    deleteBind('/foreignMain/deleteForeignMain');
    //为所有的update按钮绑定update事件
    updateBind('#toUpdateForeignMain', '/foreignMain/updateForeignMain', false, false, "#updateForeignMainModal");
    //为所有的add按钮绑定add事件
    addBind('#toAddForeignMain', '/foreignMain/addForeignMain', false, false, "#addForeignMainModal");
    //为add模态框绑定消失事件-清空模态框中内容
    $('#addForeignMainModal').on('hidden.bs.modal', function () {
        document.getElementById("toAddForeignMain").reset();
    });
    //为update模态框绑定消失事件-清空模态框中内容
    $('#updateForeignMainModal').on('hidden.bs.modal', function () {
        document.getElementById("toUpdateForeignMain").reset();
    });


    /*//为所有的delete按钮绑定delete事件
    deleteBind("#foreignMain-table-body",".data-delete","data-id",'/foreignMain/deleteForeignMain');
    //为所有的update按钮绑定update事件
    <!--String base, Long vesselNumber, Long sorties, String rescueApply, String rescueSituation, String rescueReward-->
    $("#foreignMain-table-body").on('click', ".data-update", function(){
        let data_id=$(this).attr('data-id');
        $("#updateSubmit").off().on('click', function () {
            let id = data_id;
            let data = new FormData();
            data.append("id",id);
            data.append("base",$(" #updateBase ").val());
            data.append("vesselNumber",Number($(" #updateVesselNumber ").val()));
            data.append("sorties", Number($(" #updateSorties ").val()));
            data.append("rescueApply",$(" #updateRescueApply ")[0].files[0]);
            data.append("rescueSituation",$(" #updateRescueSituation ")[0].files[0]);
            data.append("rescueReward",$(" #updateRescueReward ")[0].files[0]);
            $(this).ajaxSubmit({
                type: 'POST',
                url: '/foreignMain/updateForeignMain',
                cache: false,
                contentType: false,
                processData: false,
                dataType: "json",
                data: data,
                success: function (result) {
                    if (result.code===200){
                        $("#updatePlanModal").modal('hide');
                        $('#datatable').DataTable().ajax.reload();
                    }else {
                        $("#updatePlanModal").modal('hide');
                        $('#datatable').DataTable().ajax.reload();
                    }
                },
                error: function (result) {
                    return result;
                }
            })
        });
    });
    //为add按钮绑定add事件
    $("#toAddForeignMain").on('click', function(){
        $("#toAddForeignMain").ajaxForm(function () {
            let base = $(" #addBase ").val();
            let vesselNumber =  Number($(" #addVesselNumber ").val());
            let sorties =  Number($(" #addSorties ").val());
            let rescueApply = $(" #addRescueApply ").val();
            let rescueSituation = $(" #addRescueSituation ").val();
            let rescueReward = $(" #addRescueReward ").val();
            $(this).ajaxSubmit({
                type: 'POST',
                url: '/foreignMain/addForeignMain',
                data: { base:base,vesselNumber:vesselNumber, sorties:sorties, rescueApply:rescueApply, rescueSituation:rescueSituation, rescueReward:rescueReward},
                success: function (result) {
                    if(result.code===200){
                        window.location.reload();
                    }else{
                        console.log(result.msg);
                    }
                },
                error: function (result) {
                    return result;
                }
            })
        });
    });

    $("#addSubmit").off().on('click', function () {
        let data =new FormData();
        data.append("base",$(" #updateBase ").val());
        data.append("vesselNumber",Number($(" #updateVesselNumber ").val()));
        data.append("sorties", Number($(" #updateSorties ").val()));
        data.append("rescueApply",$(" #updateRescueApply ")[0].files[0]);
        data.append("rescueSituation",$(" #updateRescueSituation ")[0].files[0]);
        data.append("rescueReward",$(" #updateRescueReward ")[0].files[0]);
        addAjax(data,'/foreignMain/addForeignMain',false,false,'#addForeignModal');
    });*/
}
